#if 0
#ifndef WIN32

#include "Engine.h"
#include "Object.h"
#include "Draw.h"
#include "Timing.h"
#include <cstring>
#include <cstdlib>
#include <string>
#include <iostream>
#include <algorithm>

// ---------------------------------------------------------------
static Engine* engine = NULL;
// --------------------------------------------------------------------
// Attributes for 24-bit single buffered window
static int attrListSgl24bit[] = { GLX_RGBA,
				  GLX_RED_SIZE  ,  8,
			    	  GLX_GREEN_SIZE,  8,
			    	  GLX_BLUE_SIZE ,  8,
			    	  GLX_DEPTH_SIZE, 24,
			    	  GLX_STENCIL_SIZE, 8,
			    	  None };
// --------------------------------------------------------------------
// Attributes for 24-bit double buffered window
static int attrListDbl24bit[] = { GLX_RGBA, GLX_DOUBLEBUFFER,
				  GLX_RED_SIZE,    8,
				  GLX_GREEN_SIZE,  8,
				  GLX_BLUE_SIZE,   8,
				  GLX_DEPTH_SIZE, 24,
				  GLX_STENCIL_SIZE, 8,
				  None };

// ---------------------------------------------------------------
Engine::Engine(void)
    : dpy(NULL), win(0), ctx(NULL), attr(),
    running(false), width(700), height(700), imageID(0), objetos(), keyMap()
{
	memset( &attr, 0, sizeof(attr) );

    engine = this;
}
// ---------------------------------------------------------------
Engine::~Engine(void)
{
    shutdown();
}
// ---------------------------------------------------------------
Object& Engine::addObject(void)
{
    Object obj;
    objetos.push_back(obj);
    return objetos[objetos.size()-1];
}
// ---------------------------------------------------------------
int Engine::loadTexture( const char* filename )
{
	int texId = ilutGLLoadImage( (char*) filename);
	// filter aqui
	glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
	glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );

    return texId;
}
// ---------------------------------------------------------------
void Engine::loop(void)
{
    running = true;

    Timing timer;
    timer.resume();
    while ( running )
    {
        /* check for messages */
    	timer.tick();

        {
            drawScene();

            if( !handleX11Events() || getKeyState(65307) )
            	break;

            drawScene();

            // movimento de um tanque
            Object& obj = objetos[1];
            double t = 3.0/60;//timer.getElapsedTime()*0.05;


            Vetor2d eixo = Vetor2d::fromAngle( obj.angle );

            if( getKeyState(65361) ) // esquerda
            	obj.angle += 45*t;
            if( getKeyState(65362) ) // cima
            {
            	obj.posicao += eixo*0.2*t;
            }
            if( getKeyState(65363) ) // direita
            	obj.angle -= 45*t;
            if( getKeyState(65364) ) // baixo
            {
				obj.posicao -= eixo*0.2*t;
			}

            obj.posicao.x = std::min( obj.posicao.x, 0.9f );
            obj.posicao.x = std::max( obj.posicao.x,-0.9f );

            obj.posicao.y = std::min( obj.posicao.y, 0.9f );
            obj.posicao.y = std::max( obj.posicao.y,-0.9f );

            /*if( getKeyState(32) ) // rotacao
                 obj.angle += 45*t;*/

        }
    }
}
// ---------------------------------------------------------------
void Engine::resize( unsigned int width, unsigned int height )
{
	resizeWindow(width,height);
}
// ---------------------------------------------------------------
void Engine::setKeyState( int keyCode, bool state )
{
	keyMap[ keyCode ] = state;

	//std::cout << "tecla: " << keyCode << std::endl;
}
// ---------------------------------------------------------------
bool Engine::getKeyState( int keyCode )
{
	if( keyMap.find(keyCode) == keyMap.end() )
		return false;

	return keyMap[ keyCode ];
}
// ---------------------------------------------------------------
bool Engine::handleX11Events(void)
{
	XEvent event, next_event;

	while( XPending(dpy) > 0 )
	{
		XNextEvent(dpy, &event);
		switch(event.type)
		{
		case Expose:
			if (event.xexpose.count != 0)
				break;
		break;
		case ConfigureNotify:
		if ((event.xconfigure.width != width) ||
			(event.xconfigure.height != height))
		{
			resize( event.xconfigure.width, event.xconfigure.height );
		}
		break;
		case KeyPress:
		{
			int keyCode = XLookupKeysym( &event.xkey, 0);
			setKeyState( keyCode, true );
			std::cout << "press\n";
			break;
		}
		case KeyRelease:
		{
			int keyCode = XLookupKeysym( &event.xkey, 0);
			char pressed_keys[32];
			XQueryKeymap( dpy, pressed_keys);
			bool isPressed = (pressed_keys[event.xkey.keycode >> 3] >> (event.xkey.keycode & 0x07)) & 0x01;
			if (! isPressed) {
			    // the key has physically been released
				std::cout << " [true]";
				setKeyState( keyCode, false );
			}
			std::cout << "\n";

			/*
			if( XEventsQueued( dpy, QueuedAfterReading ) )
			{
				XPeekEvent( dpy, &next_event );
						if( next_event.type == KeyPress &&
							next_event.xkey.window == event.xkey.window &&
							next_event.xkey.keycode == event.xkey.keycode &&
							next_event.xkey.time == event.xkey.time )
						{
							// Do not report anything for this event
							XFlush(dpy); // thi
							break;
						}
			}*/
			break;
		}
		case ClientMessage:
		if (*XGetAtomName(dpy, event.xclient.message_type) == *"WM_PROTOCOLS")
		{
			return false;
		}
		break;
		default:
		break;
		} // SWITCH
	} // while

	return true;
}
// ---------------------------------------------------------------
bool Engine::init(const EngineParameters& params)
{
	// cria a janela no linux
	XVisualInfo *vi;
	Colormap cmap;
	int dpyWidth, dpyHeight;
	int i;
	int vidModeMajorVersion, vidModeMinorVersion;
	int modeNum;
	int bestMode;
	Atom wmDelete;
	Window winDummy;
	unsigned int borderDummy;

	bestMode = 0;
	dpy = XOpenDisplay(0);
	screen = DefaultScreen(dpy);
	vi = glXChooseVisual(dpy, screen, attrListDbl24bit );

	if(NULL == vi)
	{
		vi = glXChooseVisual(dpy, screen, attrListSgl24bit );
	}

	//
	ctx = glXCreateContext(dpy, vi, 0, GL_TRUE);
	cmap = XCreateColormap(dpy, RootWindow(dpy, vi->screen),vi->visual, AllocNone);
	attr.colormap = cmap;
	attr.border_pixel = 0;

	bool fullScreen = false;
	if( fullScreen )
	{
#if 0
		XF86VidModeSwitchToMode(dpy, screen, modes[bestMode]);
		XF86VidModeSetViewPort(dpy, screen, 0, 0);
		dpyWidth = modes[bestMode]->hdisplay;
		dpyHeight = modes[bestMode]->vdisplay;
		XFree(modes);
		attr.override_redirect = True;
		attr.event_mask = ExposureMask | KeyPressMask | KeyReleaseMask |
				  ButtonPressMask | StructureNotifyMask;

		win = XCreateWindow(    dpy, RootWindow(dpy, vi->screen),
					0, 0, dpyWidth, dpyHeight,
					0, vi->depth, InputOutput, vi->visual,
					CWBorderPixel | CWColormap | CWEventMask | CWOverrideRedirect,
				&attr);

		XWarpPointer(dpy, None, win, 0, 0, 0, 0, 0, 0);
		XMapRaised(dpy, win);
		XGrabKeyboard(dpy, win, True, GrabModeAsync,GrabModeAsync, CurrentTime);
		XGrabPointer(dpy, win, True, ButtonPressMask, GrabModeAsync,
				 GrabModeAsync, win, None, CurrentTime);
#endif
	}
	else
	{
		attr.event_mask = ExposureMask | KeyPressMask | KeyReleaseMask |
				  ButtonPressMask | StructureNotifyMask;

		win = XCreateWindow(    dpy, RootWindow(dpy, vi->screen),
					0, 0, width, height, 0, vi->depth,
					InputOutput, vi->visual,
					CWBorderPixel | CWColormap | CWEventMask, &attr);

		/*XSelectInput( dpy, win,
FocusChangeMask | KeyPressMask | KeyReleaseMask
| PropertyChangeMask | StructureNotifyMask | KeymapStateMask);*/

		//XSelectInput( dpy, DefaultRootWindow(dpy), KeyPressMask | KeyReleaseMask );
		XGrabKeyboard( dpy, DefaultRootWindow(dpy), False, GrabModeAsync, GrabModeAsync, CurrentTime);

		std::string name = "Baby Engine";
		wmDelete = XInternAtom(dpy, "WM_DELETE_WINDOW", True);
		XSetWMProtocols(dpy, win, &wmDelete, 1);
		XSetStandardProperties(dpy, win, name.c_str(),
					name.c_str(), None, NULL, 0, NULL);
		XMapRaised(dpy, win);
	}

	int wx, wy;
	unsigned int xwidth, xheight, wbpp;
	glXMakeCurrent(dpy, win, ctx);
	XGetGeometry(dpy, win, &winDummy, &wx, &wy,
			 &xwidth, &xheight, &borderDummy, &wbpp);


	if( glXIsDirect(dpy, ctx) )
	{
	   printf("Direct Rendering!\n" );
	}
	else
	{
	   printf("Software Rendering! Check your video driver!\n" );
	}

	// OpenIL
	ilutRenderer(ILUT_OPENGL);
	ilInit();
	iluInit();
	ilutInit();
	ilutRenderer(ILUT_OPENGL);

	ilGenImages( 1, &imageID );
	ilBindImage( imageID );


    return true;
}
// ---------------------------------------------------------------
void Engine::enableOpenGL(void)
{

}
// ---------------------------------------------------------------
void Engine::shutdown(void)
{
    ilDeleteImage( imageID );
     imageID = 0;

    // OpenIL
    ilShutDown();

    //dtor
#ifdef WIN32
    if( hRC )
    {
        wglMakeCurrent(NULL, NULL);
        wglDeleteContext(hRC);
        hRC = NULL;
    }

    if( hwnd && hDC )
    {
        ReleaseDC(hwnd, hDC);
        hDC = NULL;
    }

    /* destroy the window explicitly */
    if(hwnd)
    {
            DestroyWindow(hwnd);
            hwnd = NULL;
    }
#else
    // TODO fazer o mesmo no Linux
#endif
}

static float px = 0, py = 0;
// ---------------------------------------------------------------
void Engine::resizeWindow( unsigned int _width, unsigned int _height )
{
    width  = _width;
    height = _height;

    glViewport(0,0,width, height);

    // NOTA: melhora o desempenho
    //glEnable(GL_SCISSOR_TEST);
    //glScissor( 0,0, width, height);


    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();    // rescala o Y
    float sy = (height) ? ((float)width/(float)height): 1.0f;
    glScalef( 1.0f, sy, 1.0f);
    glMatrixMode(GL_MODELVIEW);
}


// ---------------------------------------------------------------
void Engine::drawScene(void)
{
    static float theta =0;

    glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
    glClear(GL_COLOR_BUFFER_BIT);
    glLoadIdentity();

    for( unsigned int i=0; i<objetos.size() ; ++i )
    {
        glPushMatrix();
            glLoadIdentity();
            objetos[i].draw();
        glPopMatrix();
    }

    static Material opaco;
    opaco.cor.set(0.75f,1,0.75f,0.9f);
    opaco.texturaHabilitada = false;
    opaco.usarMaterial();

    static float angle = 45;

    angle += 0.6f;

    glFlush();
#ifdef WIN32
    SwapBuffers(hDC);
#else
    // TODO disparar a troca dos buffers no windows
    glXSwapBuffers(dpy, win);
#endif

    theta += 1.0f;
    //Sleep (1);
}
// ---------------------------------------------------------------
void Engine::drawQuad(void)
{
    // TODO dar uma cor
    glBegin(GL_QUADS);
        //glColor3f(1.0f, 1.0f, 1.0f);
        //const Cor& cor = Cor::COLOR_GREEN;
        //glColor4f( cor.r, cor.g, cor.b, cor.a );

        glTexCoord2f(0.0f,0.0f);        glVertex2f( -1.0f, -1.0f);
        glTexCoord2f(1.0f,0.0f);        glVertex2f( +1.0f, -1.0f);
        glTexCoord2f(1.0f,1.0f);        glVertex2f( +1.0f, +1.0f);
        glTexCoord2f(0.0f,1.0f);        glVertex2f( -1.0f, +1.0f);

    glEnd();
}
// ---------------------------------------------------------------
#endif
#endif
